package com.twu.biblioteca;

import java.util.NoSuchElementException;

public class BibliotecaApp {

    public static void main(String[] args) {
        MainMenu activeMenu = new BibliotecaApp().createMainMenu();
        RecordList recordList = new RecordList(new Book(0, "testbook", RecordState.AVAILABLE), new Book(1, "testbook1", RecordState.CHECKED_OUT), new Movie(3, "First Movie", RecordState.AVAILABLE, 1984, "expectedDirector", RecordRating.ONE), new Movie(4, "Second Movie", RecordState.CHECKED_OUT, 1984, "expectedDirector", RecordRating.TWO) );
        Authenticator authenticator = new BibliotecaApp().createAuthenticator();

        System.out.println("+++++++ "+ new WelcomeMessage().getMessage() +" ++++++++1");
        while(true) {
            int userSelection = 0;

            try {
                userSelection = activeMenu.mainMenuLauncher();
            }
            catch (NoSuchElementException | NumberFormatException e) {
                System.out.println("your entry is not valid. Select a valid option!:");
            }

            switch (userSelection) {

                case 1:
                    System.out.println(new ListFormatter(recordList.getAvailableBookList()).format());
                    break;

                case 2:
                    System.out.println(new ListFormatter(recordList.getAvailableMovieList()).format());
                    break;

                case 3:
                    if(!authenticator.isLoggedIn()){authenticator.assureLogin();}

                    try {
                        int recordId = activeMenu.recordCheckout();
                        Record record = recordList.getRecordById(recordId);
                        recordList.checkout(record);
                        System.out.println("Thank you! Enjoy the book or movie");
                    } catch (NoSuchElementException e) {
                        System.out.println("That book or movie is not available");
                    } catch (UnsupportedOperationException e) {
                        System.out.println("That item has already been checked out!");
                    }

                    break;

                case 4:
                    if(!authenticator.isLoggedIn()){authenticator.assureLogin();}

                    try {
                        int recordId = activeMenu.returnRecord();
                        Record record = recordList.getRecordById(recordId);
                        recordList.giveBack(record);
                        System.out.println("Thank you for returning the item");
                    } catch (IndexOutOfBoundsException e) {
                        System.out.println("That is not a valid item to return");
                    } catch (UnsupportedOperationException e) {
                        System.out.println("That item is currently in the library!" + e.getMessage());
                    }
                    break;

                case 5:
                    if(!authenticator.isLoggedIn()){authenticator.assureLogin();}
                    System.out.println(authenticator.getUserInformation());
                    break;

                case 9:
                    System.out.println("You are exiting Biblioteca. Good bye");
                    System.exit(0);
                    break;

                default:
                    throw new NoSuchElementException();
            }

            }

        }

    private Authenticator createAuthenticator() {
        User currentUser = new User(1111111, "password", "testname", "test-email", 1234567);
        UserList userList = new UserList();
        userList.add(currentUser);
        Login login = new Login(System.in, System.in, userList);
        return new Authenticator(null, login);
    }

    private MainMenu createMainMenu() {
        return new MainMenu(System.in);
    }

}
